package com.pos.clothes.controller;

import com.github.pagehelper.PageInfo;
import com.pos.clothes.pojo.Category;
import com.pos.clothes.service.CategoryService;
import com.pos.clothes.service.serviceImpl.CategoryServiceImpl;
import com.pos.clothes.util.JsonData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiresPermissions("3")
public class CategoryController {
    @Autowired
    private CategoryService service;

    @Autowired
    private Category category;
    @RequestMapping("/showCategory")
    @ResponseBody
    public List<Category> getCategory(){

        List<Category> categories = service.getCategory();
        return categories;
    }


    @RequestMapping("/chaxun")
    @ResponseBody
    public List<Category> searchCategory(@RequestParam("category_name") String category_name){
        List<Category> categorys = service.searchCategory(category_name);
        return categorys;
    }



    @RequestMapping("/shanchu")
    @ResponseBody
    public Object deleteCategory(@RequestParam("id") int id){
        int res = service.deleteCategory(id);
        return JsonData.buildSuccess(res,"ok");
    }

    @RequestMapping("/tianjia")
    @ResponseBody
    public Object insertCategory(@RequestParam("category_name") String category_name,@RequestParam("category_note") String category_note){

        if(category_name==""){
            return JsonData.buildError("类别名称不能为空");
        }
        List<Category> categorys = service.searchCategory(category_name);
        if(!categorys.isEmpty()){
            return JsonData.buildError("类别名称已经存在");
        }

        Category category = new Category();
        category.setCategory_name(category_name);
        category.setCategory_note(category_note);
        service.insertCategory(category);
        return JsonData.buildSuccess(category.getId(),"ok");
    }

    @RequestMapping("/gengxin")
    @ResponseBody
    public Object updateCategory(@RequestParam("id") int id,@RequestParam(value = "category_name") String category_name,@RequestParam(value = "category_note") String category_note){

        if(category_name==""){
            return JsonData.buildError("类别名称不能为空");
        }

        if(category_note==""){
            return JsonData.buildError(("类别说明不能为空"));
        }

        List<Category> categorys = service.searchCategory(category_name);
        if(!categorys.isEmpty()) {
            if(categorys.get(0).getId()!=id ) {
                return JsonData.buildError("类别已存在");

            }

        }


        Category category = new Category();
        category.setId(id);
        category.setCategory_name(category_name);
        category.setCategory_note(category_note);
        service.updateCategory(category);
        return JsonData.buildSuccess(category.getId(),"ok");
    }


    @RequestMapping(value = "/showCategory1")
    @ResponseBody
    public PageInfo getGoodsTypeList( int pageNum, int pageSize) throws Exception {
        return service.getList(pageNum,pageSize);
    }

    @RequestMapping("/chaxun1")
    @ResponseBody
    public PageInfo searchCategory(@RequestParam("category_name") String category_name,@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize) throws Exception {
        PageInfo categorys = service.searchCategory1(category_name,pageNum,pageSize);
        return categorys;
    }



}
